/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.ulss.datadispatch.commons;

import cn.ac.iie.ulss.datadispatch.config.Configuration;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author AlexMu
 */
public class RuntimeEnv {

    private static Configuration conf = null;
    public static final String NAME_SERVER_LIST = "nameServerList";
    public static final String TOPIC = "topic";
    public static final String JSONFILE = "jsonfile";
    public static final String TABLENAME = "tablename";
    public static final String GROUPNAME= "groupname";
    private static Map<String, Object> dynamicParams = new HashMap<String, Object>();

    public static boolean initialize(Configuration pConf) throws Exception {

        if (pConf == null) {
            System.out.println("configuration object is null");
            return false;
        }
        conf = pConf;

        getConfAndSaveAsString(NAME_SERVER_LIST, false);
        getConfAndSaveAsString(TOPIC, false);
        getConfAndSaveAsString(JSONFILE, false);
        getConfAndSaveAsString(TABLENAME, false);
        getConfAndSaveAsString(GROUPNAME, true);
        return true;
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    //fixme
    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }

    //fixme
    public static String getParamAsString(String pParamName) {
        return (String) dynamicParams.get(pParamName);
    }

    //fixme
    public static Integer getParamAsInteger(String pParamName) {
        return (Integer) dynamicParams.get(pParamName);
    }

    //fixme
    public static Boolean getParamAsBoolean(String pParamName) {
        return (Boolean) dynamicParams.get(pParamName);
    }

    public static void getConfAndSaveAsInteger(String confName) throws Exception {
        String tmp = conf.getString(confName, "");
        if (tmp.isEmpty()) {
            throw new Exception("parameter " + confName + " does not exist or is not defined");
        }
        try {
            dynamicParams.put(confName, Integer.parseInt(tmp));
        } catch (Exception ex) {
            throw new Exception("parameter " + confName + " 's format is wrong for " + ex.getMessage(), ex);
        }
    }

    public static void getConfAndSaveAsString(String confName, boolean allowEmpty) throws Exception {
        String tmp = conf.getString(confName, "");
        if (tmp.isEmpty() && !allowEmpty) {
            throw new Exception("parameter" + confName + "does not exist or is not defined");
        }
        dynamicParams.put(confName, tmp);
    }

    public static void getConfAndSaveAsBoolean(String confName) throws Exception {
        String tmp = conf.getString(confName, "");
        if (tmp.isEmpty()) {
            throw new Exception("parameter " + confName + " does not exist or is not defined");
        }
        try {
            dynamicParams.put(confName, Boolean.parseBoolean(tmp));
        } catch (Exception ex) {
            throw new Exception("parameter " + confName + " 's format is wrong for " + ex.getMessage(), ex);
        }
    }

    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }
}
